package com.example.inventoryservice.company.strategy;


import com.example.inventoryservice.company.CompanyFinancialData;
import com.example.inventoryservice.company.IncomeTaxStrategy;
import com.example.inventoryservice.company.TaxCalculationResult;

import java.math.BigDecimal;

public class GeneralEnterpriseTaxStrategy implements IncomeTaxStrategy {
    @Override
    public TaxCalculationResult calculate(CompanyFinancialData data) {
        // 一般企业适用25%基本税率
        BigDecimal taxableIncome = data.getTaxableIncome();
        BigDecimal taxRate = new BigDecimal("0.25");
        BigDecimal baseTax = taxableIncome.multiply(taxRate);

        // 环保设备投资抵免
        BigDecimal ecoEquipmentDeduction = calculateEcoDeduction(data);
        BigDecimal deductions = ecoEquipmentDeduction;

        BigDecimal finalTax = baseTax.subtract(deductions);
        // 确保税额不为负数
        if (finalTax.compareTo(BigDecimal.ZERO) < 0) {
            finalTax = BigDecimal.ZERO;
        }

        BigDecimal prepaidTax = data.getQuarterlyTaxPrepayments().stream().reduce(BigDecimal.ZERO, BigDecimal::add);

        return new TaxCalculationResult(taxableIncome, taxRate, baseTax, deductions, finalTax, prepaidTax);
    }

    private BigDecimal calculateEcoDeduction(CompanyFinancialData data) {
        // 模拟环保设备投资抵免计算
        return data.getTotalAssets().multiply(new BigDecimal("0.02")); // 假设资产的2%用于环保设备
    }

    @Override
    public boolean isApplicable(CompanyFinancialData data) {
        return "GENERAL_ENTERPRISE".equals(data.getCompanyType());
    }
}
